//
//  BA_JobManager.m
//  BaseProject
//
//  Created by 王洋 on 2021/4/21.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "BA_JobManager.h"

@interface BA_JobManager ()

@property (nonatomic,strong) NSLock *lock;

@property (nonatomic, strong) NSMutableArray <JobModel *>*jobArray;

@end

@implementation BA_JobManager

+ (BA_JobManager *)manager {
    static BA_JobManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        manager = [BA_JobManager new];
        manager.jobArray = @[].mutableCopy;
//        manager.lock =[[NSLock alloc] init];
    });
    return manager;
}

- (void)addJobWithModel:(JobModel *)model {
//    //    [self.lock lock];;
    [self.jobArray addObject:model];
//    //    [self.lock unlock];
}

- (void)addSingleJobWithModel:(JobModel *)model {
    //    [self.lock lock];;
    NSMutableArray <JobModel *>*tempArray = self.jobArray.mutableCopy;
    for (NSInteger i = tempArray.count-1; i >=0; i--) {
        if (tempArray[i].jobType == model.jobType) {
            [self.jobArray removeObjectAtIndex:i];
        }
    }
    [self.jobArray addObject:model];
    //    [self.lock unlock];
}

//更新job   一般是更新激活状态
- (void)updateJobWithModel:(JobModel *)model {
    //    [self.lock lock];;
    NSMutableArray <JobModel *>*tempArray = self.jobArray.mutableCopy;
    for (NSInteger i = tempArray.count-1; i >=0; i--) {
        if (tempArray[i].jobType == model.jobType) {
            [self.jobArray removeObjectAtIndex:i];
        }
    }
    [self.jobArray addObject:model];
    //    [self.lock unlock];
}

- (JobModel *)getJobWithType:(BA_JobManagerType)type {
    JobModel *getModel = [JobModel new];
    for (JobModel *model in self.jobArray) {
        if (model.jobType == type) {
            getModel = model;
        }
    }
    return getModel;
}

//移除某一类job
- (void)removeJobWithType:(BA_JobManagerType)type {
    //    [self.lock lock];;
    NSMutableArray <JobModel *>*tempArray = self.jobArray.mutableCopy;
    for (NSInteger i = tempArray.count-1; i >=0; i--) {
        if (tempArray[i].jobType == type) {
            [self.jobArray removeObjectAtIndex:i];
        }
    }
    //    [self.lock unlock];
}

//检查某个job要不要触发
- (void)checkTriggerWithType:(BA_JobManagerType)type comption:(void (^)(BOOL success))comption {

    if (self.jobArray.count == 0) {
        comption(NO);
        return;
    }
    
    //    [self.lock lock];;
    
    NSInteger aimIndex = 0;
    for (int i = 0; i < self.jobArray.count; i++) {
        if (self.jobArray[i].jobType == type) {
            aimIndex = i;
        }
    }

    if (self.jobArray[aimIndex].jobType == type && self.jobArray[aimIndex].triggerState == JobTriggerType_wait) {
        comption(YES);
    } else {
        comption(NO);
    }
    //    [self.lock unlock];
}

//慎用，仅用于退出登录切换账号
- (void)clearAllJobs {
    //    [self.lock lock];;
    self.jobArray = @[].mutableCopy;
    //    [self.lock unlock];
}

- (NSMutableArray<JobModel *> *)jobArray {
    if (!_jobArray) {
        _jobArray = @[].mutableCopy;
    }
    return _jobArray;
}

@end
